<script type="text/javascript">
var <?= $this->jqgrid->jsObject ?> = {

    gridId: null,

    rowId: null,

    model: null,

    idPos: 4,

    emptyData: {
        id: 0,
        cell: ["- нет доступных привилегий -"]
    },

	loadData: {
		id: 0,
		cell: ["идет загрузка..."]
	},

    options: <?= $this->subjqgrid->renderOptions() ?>,

    setPrivileges: function(container, rowId) {

        var obj = this;

		$(container).attr('name', 'id');
		obj.addOption(container, obj.loadData);

        var url = $.exReqPath(obj.options.url) + "?" + $.param( $.extend($.exReqQuery(obj.options.url), {diff: rowId}) );

		$.getJSON(url, null, function(data) {
			if (!data || typeof data != 'object') {
				return;
			}

			$(container).empty();
			
			if (undefined !== data.rows) {
                if (data.rows.length == 0) {
                    data.rows[0] = obj.emptyData;
                } 

				for (var i = 0; i < data.rows.length; i++) {
					obj.addOption(container, data.rows[i]);
				}
			}
		});
    },

    addOption: function(container, row) {
        $(container).append($("<option value=\"" + row.id + "\">" + row.cell[0] + "</option>"));
    },

    deletePrivilege: function(container) {
        var obj = this;
		
        $(container).find("option:selected").remove();

        if ($(container).find("option").length < 1) {
            obj.addOption(container, obj.emptyData);
        }
    },

	expand: function(subgridId, rowId) {
        var obj = this;
        obj.rowId = rowId;

		var grid = $("#<?= $this->jqgrid->tableId ?>");
		var rowData = grid.jqGrid('getRowData', rowId);

        var tableId = obj.gridId = subgridId + 't';
        var pagerId = subgridId + 'p';

        var options = $.extend({}, obj.options);
        
        options = $.extend(options, {
            url: options.url + '?' + $.param({id: rowId}),
            pager: pagerId,
            editurl: options.editurl + '?' + $.param({nodeid: rowId})
        });

        $("#" + subgridId).html("<div style='padding:5px'><table id='" + tableId + "' class='scroll'></table><div id='" + pagerId + "'></div></div>");
        $("#" + tableId).jqGrid(options);

        <?= $this->subjqgrid->renderNavGrid('"#" + tableId', '"#" + pagerId', true) ?>;

        $("#" + tableId).jqGrid("navButtonAdd", "#" + pagerId, {
            buttonicon: 'ui-icon-plus',
            caption: '',
            position: 'first',
            onClickButton: function() {
               obj.addPrivilege(tableId, rowId);
            }
        });
	},

	collapse: function(subgridId, rowId) {},
    
    addPrivilege: function(gridId, rowId) {
		var obj = this;

        obj.model = $("#" + gridId ).jqGrid('getGridParam','colModel');

		if (undefined === obj.model[obj.idPos]) {
			return;
		}

		var col = obj.model[obj.idPos];
        col.hidden = false;

        $("#" + gridId).jqGrid('editGridRow', 'new', {
            onInitializeForm: function(form) {
                $(form.selector).find("select").each(function() {
                    obj.setPrivileges(this, rowId);
                });
            },
            afterComplete: function(resp, data, form) {
                obj.deletePrivilege(form[0].id);
            },
            beforeSubmit: function(data, form) {
                if (data.id < 1) {
                    return [false, "Не указана привилегия для добавления"]
                } else {
                    return [true];
                }
            }
            //errorTextFormat: function(resp) {}
        });
    }
};

</script>
<?= $this->jqgrid; ?>

